package com.song.osclient;

import android.app.IntentService;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;

import com.song.osclient.util.PrintLog;

/**
 * Created by Administrator on 2015/12/9.
 */
public class DeleteSmsService extends IntentService {

    public DeleteSmsService() {
        super("deleteSmsService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent == null) return;
        PrintLog.error("qian ------------------------->>>>>" + ":" + getAllMessageByNumber());
//        deleteSMS(intent.getStringExtra("content"));
//        PrintLog.error("qian ------------------------->>>>>" + ":" + getAllMessageByNumber());
    }

    public void deleteSMS(String content) {
        try {
            Uri uri = Uri.parse("content://sms/inbox");// 收信箱
            ContentResolver contentResolver = getContentResolver();
            Cursor isRead = contentResolver.query(uri, new String[]{"_id", "address", "body",}, "address=?", new String[]{"10086"}, null);
            if (isRead == null) return;
            while (isRead.moveToNext()) {
                String body = isRead.getString(isRead.getColumnIndex("body"));// 获取信息内容
                if (body.equals(content)) {
                    int id = isRead.getInt(isRead.getColumnIndex("_id"));
//                    contentResolver.delete(Uri.parse("content://sms"), "_id=" + id, null);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getAllMessageByNumber() {
        StringBuilder builder = new StringBuilder();
        final String SMS_URI_INBOX = "content://sms/inbox";// 收信箱 /*content://sms/inbox 收件箱 content://sms/sent 已发送 content://sms/draft 草稿 content://sms/outbox 发件箱 content://sms/failed 发送失败 content://sms/queued 待发送列表*/
        ContentResolver cr = getContentResolver();
        String[] projection = new String[]{"_id", "address", "body",};
        Uri uri = Uri.parse(SMS_URI_INBOX);
        Cursor cursor = cr.query(uri, projection, "address=?", new String[]{"10086"}, null/*降序 升序把 desc 去掉即可*/);
        if (cursor == null) return "";
        while (cursor.moveToNext()) {
            int contentColumn = cursor.getColumnIndex("body");// 短信内容
            builder.append(cursor.getString(contentColumn)).append("\n");
        }
        cursor.close();
        return builder.toString();
    }
}
